package com.xmtlxx.wms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.wms.domain.bo.InventoryLogBo;
import com.xmtlxx.wms.domain.vo.InventoryLogVo;
import com.xmtlxx.wms.service.IInventoryLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 库存记录Controller
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Validated
@Api(value = "库存记录控制器", tags = {"库存记录管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/wms/inventoryLog")
public class InventoryLogController extends BaseController {

    private final IInventoryLogService iInventoryLogService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<InventoryLogVo> initBean(){
        InventoryLogVo vo = new InventoryLogVo();
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询库存记录列表
     */
    @ApiOperation("查询库存记录列表")
    @SaCheckPermission("wms:inventoryLog:list")
    @GetMapping("/list")
    public TableDataInfo<InventoryLogVo> list(@Validated(QueryGroup.class) InventoryLogBo bo, PageQuery pageQuery) {
        return iInventoryLogService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出库存记录列表
     */
    @ApiOperation("导出库存记录列表")
    @SaCheckPermission("wms:inventoryLog:export")
    @Log(title = "库存记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated InventoryLogBo bo, HttpServletResponse response) {
        List<InventoryLogVo> list = iInventoryLogService.queryList(bo);
        ExcelUtil.exportExcel(list, "库存记录", InventoryLogVo.class, response);
    }

    /**
     * 获取库存记录详细信息
     */
    @ApiOperation("获取库存记录详细信息")
    @SaCheckPermission("wms:inventoryLog:query")
    @GetMapping("/{id}")
    public R<InventoryLogVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") String id) {
        return R.ok(iInventoryLogService.queryById(id));
    }

    /**
     * 新增库存记录
     */
    @ApiOperation("新增库存记录")
    @SaCheckPermission("wms:inventoryLog:add")
    @Log(title = "库存记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody InventoryLogVo vo) {
        InventoryLogBo bo = new InventoryLogBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iInventoryLogService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改库存记录
     */
    @ApiOperation("修改库存记录")
    @SaCheckPermission("wms:inventoryLog:edit")
    @Log(title = "库存记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody InventoryLogVo vo) {
        InventoryLogBo bo = new InventoryLogBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iInventoryLogService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除库存记录
     */
    @ApiOperation("删除库存记录")
    @SaCheckPermission("wms:inventoryLog:remove")
    @Log(title = "库存记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iInventoryLogService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
